package com.newzqxq.koalamoney.presenter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.newzqxq.koalamoney.common.App;
import com.newzqxq.koalamoney.common.BasePresenter;
import com.newzqxq.koalamoney.common.SuperPresenter;
import com.newzqxq.koalamoney.entity.RegisterBean;
import com.newzqxq.koalamoney.model.RegisterModel;
import com.newzqxq.koalamoney.net.Api;
import com.newzqxq.koalamoney.net.DaggerModelComponent;
import com.newzqxq.koalamoney.ui.activity.RegiserActivity;
import com.newzqxq.koalamoney.utils.MyToast;
import com.newzqxq.koalamoney.view.IRegisterView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import happyloan.core.RespEvent;

/**
 * 注册
 * Created by ZQXQ-Developer on 2017/7/24.
 */

public class RegisterPresenter extends SuperPresenter {
    private IRegisterView userRegisterView;

    private Context mContext;

    @Inject
    RegisterModel userRegisterModel;

    public RegisterPresenter(Context context, IRegisterView userRegisterView) {
        this.mContext = context;
        this.userRegisterView = userRegisterView;
        setupDagger2();
    }

    private void setupDagger2() {
        DaggerModelComponent.builder().appComponent(App.getInstance().getAppComponent()).build().inject(this);
    }

    public void doRegister(String phone, String code, int type, String password) {
        userRegisterModel.register(phone, code, password, type);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void doRegister(RespEvent<RegisterBean.DataBean> respEvent) {
        if (respEvent.getUrl().equals(Api.register)) {
            switch (respEvent.getCode()) {
                case RespEvent.Code.SUCCEED:
                    userRegisterView.onRegisterResult(respEvent.getT());
                    break;
                case RespEvent.Code.FAILURE:
                    if (respEvent.isAlert()) {
                        Log.i("TAG", "doRegister: " + respEvent.getExtra());
                    }
                    break;
                case RespEvent.Code.ERROR:
                    MyToast.shortToast(mContext, "请求失败，请重试！");
                    break;
            }
        }
    }

    public void getCode(String phone, int type) {
        userRegisterModel.sendCode(phone, type);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getCode(RespEvent<RegisterBean> respEvent) {
        if (respEvent.getUrl().equals(Api.rgGetCode)) {
            switch (respEvent.getCode()) {
                case RespEvent.Code.SUCCEED:
                    Log.i("TAG", "doRegister11: " + respEvent.getExtra());
                    break;
                case RespEvent.Code.FAILURE:
                    if (respEvent.isAlert()) {
                        MyToast.shortToast(mContext, respEvent.getExtra());
                    }
                    break;
                case RespEvent.Code.ERROR:
                    MyToast.shortToast(mContext, "请求失败，请重试！");
                    break;
            }
        }
    }

    @Override
    public void start() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void end() {
        EventBus.getDefault().unregister(this);
    }
}
